package com.foodorder.repository;

import com.foodorder.entity.Banner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Banner数据访问层
 */
@Repository
public interface BannerRepository extends JpaRepository<Banner, Long> {
    
    /**
     * 根据状态查找Banner列表，按排序号排序
     */
    List<Banner> findByStatusOrderBySortOrderAsc(Banner.BannerStatus status);
    
    /**
     * 根据标题模糊查询
     */
    @Query("SELECT b FROM Banner b WHERE b.title LIKE %:title% ORDER BY b.sortOrder ASC")
    Page<Banner> findByTitleContaining(@Param("title") String title, Pageable pageable);
    
    /**
     * 根据状态和标题查询
     */
    @Query("SELECT b FROM Banner b WHERE b.status = :status AND b.title LIKE %:title% ORDER BY b.sortOrder ASC")
    Page<Banner> findByStatusAndTitleContaining(
            @Param("status") Banner.BannerStatus status, 
            @Param("title") String title, 
            Pageable pageable
    );
    
    /**
     * 获取最大排序号
     */
    @Query("SELECT COALESCE(MAX(b.sortOrder), 0) FROM Banner b")
    Integer findMaxSortOrder();
} 